package com.hades.xq.springcool.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration //让SpringBoot自动加载类内的配置
public class FastJsonConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig configuration = new FastJsonConfig();
        configuration.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
        fastJsonHttpMessageConverter.setFastJsonConfig(configuration);
        converters.add(fastJsonHttpMessageConverter);
    }
}
/**
 * FastJson SerializerFeatures:
 * WriteNullListAsEmpty  ：List字段如果为null,输出为[],而非null
 * WriteNullStringAsEmpty ： 字符类型字段如果为null,输出为"",而非null
 * DisableCircularReferenceDetect ：消除对同一对象循环引用的问题，默认为false（如果不配置有可能会进入死循环）
 * WriteNullBooleanAsFalse：Boolean字段如果为null,输出为false,而非null
 * WriteMapNullValue：是否输出值为null的字段,默认为false。
 */


